# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=lxml
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=4.9.3
pkgrel=1
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}=${pkgver}"
          "${MINGW_PACKAGE_PREFIX}-python-${_realname}-docs")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}"
           "${MINGW_PACKAGE_PREFIX}-python-${_realname}-docs")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}"
          "${MINGW_PACKAGE_PREFIX}-python-${_realname}-docs")
pkgdesc="Python binding for the libxml2 and libxslt libraries (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
license=('spdx:BSD-3-Clause')
url="https://lxml.de/"
depends=("${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libxslt"
         "${MINGW_PACKAGE_PREFIX}-python")
optdepends=("${MINGW_PACKAGE_PREFIX}-python-beautifulsoup4: support for parsing not well formed HTML"
            "${MINGW_PACKAGE_PREFIX}-python-cssselect: support for cssselect"
            "${MINGW_PACKAGE_PREFIX}-python-html5lib: support for html5lib parser")
makedepends=("${MINGW_PACKAGE_PREFIX}-cython"
             "${MINGW_PACKAGE_PREFIX}-python-docutils"
             "${MINGW_PACKAGE_PREFIX}-python-beautifulsoup4"
             "${MINGW_PACKAGE_PREFIX}-python-cssselect"
             "${MINGW_PACKAGE_PREFIX}-python-html5lib"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-cc")
source=("https://github.com/lxml/lxml/archive/${_realname}-${pkgver}.tar.gz"
        "use-distutils-get_platform.patch"
        "0001-setupinfo-fix-library-name.patch"
        "https://github.com/lxml/lxml/commit/53eb0f82641cbfc968f0fb8ca6d81767fcfe1a5b.patch")
sha256sums=('42b9ab83cb8739d817c7fff41c20f31aa61625bb6f6ab333873a5f3406b139ac'
            '6e58bd3072a6a77bf50c4c203beef589c97873e9902d4c8c6151abe71035f186'
            '7d9e5380a8461e3c2bb4b8e0ece52011b26e3112b94b97e548c927524cfc93de'
            '27b1051219a7036938065d67cc54a8bcc838e0de79d748ddff03fced83ea03ef')

prepare() {
  pushd "${_realname}-${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}/use-distutils-get_platform.patch"
  patch -p1 -i "${srcdir}/0001-setupinfo-fix-library-name.patch"
  patch -p1 -i "${srcdir}/53eb0f82641cbfc968f0fb8ca6d81767fcfe1a5b.patch"
  popd

  rm -rf python-build-${MSYSTEM} | true
  cp -r "${_realname}-${_realname}-${pkgver}" "python-build-${MSYSTEM}"
}

build() {
  msg "Python build for ${MSYSTEM}"
  cd "${srcdir}/python-build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/python setup.py build \
       --with-cython --with-unicode-strings
}

package() {
  cd "${srcdir}/python-build-${MSYSTEM}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX#\/} \
        --root="${pkgdir}" --optimize=1 --skip-build \
        --with-cython --with-unicode-strings

  install -Dm644 LICENSES.txt \
    "${pkgdir}${MINGW_PREFIX}"/share/licenses/python-${_realname}/LICENSE
  install -Dm644 doc/licenses/BSD.txt \
    "${pkgdir}${MINGW_PREFIX}"/share/licenses/python-${_realname}/BSD.txt
  install -Dm644 doc/licenses/elementtree.txt \
    "${pkgdir}${MINGW_PREFIX}"/share/licenses/python-${_realname}/elementtree.txt
}
